import pandas as pd
import matplotlib.pyplot as plt

# 读取CSV文件
file_path = '../data/橘子洲_emotion_analysis.csv'
data = pd.read_csv(file_path)

# 显示数据的前几行以检查导入是否正确
print(data.head())

# 柱状图：长度分布
plt.figure(figsize=(10, 6))
plt.bar(data['length'].value_counts().index, data['length'].value_counts().values)
plt.xlabel('Length')
plt.ylabel('Frequency')
plt.title('Length Distribution')
plt.savefig('../data2/橘子洲_length_distribution.png')  # 保存柱状图为PNG文件
plt.show()

# 饼图：情感评分分布
# 计算每个情感评分的平均值
totals =  data[['positive', 'negative']].mean()

# 计算所有情感评分的总和
total_sum = totals.sum()

# 计算每个情感评分的百分比
percentages = (totals / total_sum) * 100

# 绘制饼图
plt.figure(figsize=(8, 8))
plt.pie(percentages, labels=percentages.index, autopct='%1.1f%%', startangle=140)
plt.title('Emotional Ratings Percentage')
plt.savefig('../data2/橘子洲_Emotional Ratings Percentage.png')
plt.show()

# 计算每个情感评分的总和
totals = data[['anger', 'disgust', 'fear', 'sadness', 'surprise', 'good', 'happy']].sum()

# 计算所有情感评分的总和
total_sum = totals.sum()

# 计算每个情感评分的百分比
percentages = (totals / total_sum) * 100

# 绘制饼图
plt.figure(figsize=(8, 8))
plt.pie(percentages, labels=percentages.index, autopct='%1.1f%%', startangle=140)
plt.title('Emotional Ratings Percentage')
plt.savefig('../data2/橘子洲_Emotional Ratings Percentage ALL.png')
plt.show()


# 柱状图：每个情感评分的分布
plt.figure(figsize=(12, 8))
for i, column in enumerate(data[['positive', 'negative', 'anger', 'disgust','fear','sadness','surprise','good','happy']].columns):
    plt.subplot(3, 3, i+1)
    plt.hist(data[column], bins=5, alpha=0.7, label=column,rwidth=0.6,color='blue')
    plt.title(column)
    plt.legend()
plt.tight_layout()
plt.savefig('../data2/橘子洲_emotion_ratings_distribution.png')  # 保存情感评分分布图为PNG文件
plt.show()